﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.SalesCommand;
using Yitter.IdGenerator;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.SalesCommandHandler
{
    /// <summary>
    /// 添加发货通知单命令处理
    /// </summary>
    public class AddDeliveryNoticeCommandHandler : IRequestHandler<AddDeliveryNoticeCommand, APIResult<int>>
    {
        /// <summary>
        /// 发货通知单仓储
        /// </summary>
        private readonly IBaseRepository<DeliveryNotice> deliverRep;
        private readonly IBaseRepository<OrderMaterials> materialBiz;
        private readonly IMapper mapper;

        public AddDeliveryNoticeCommandHandler(IBaseRepository<DeliveryNotice> deliverRep, IBaseRepository<OrderMaterials> materialBiz, IMapper mapper)
        {
            this.deliverRep = deliverRep;
            this.materialBiz= materialBiz;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<APIResult<int>> Handle(AddDeliveryNoticeCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            DeliveryNotice delivery=new DeliveryNotice();
            delivery.NoticeId = Guid.NewGuid();
            delivery.DocumentCode = YitIdHelper.NextId().ToString();
            delivery.DocumentDate = DateTime.Now;
            delivery.BusinessTypeId = request.BusinessTypeId;
            delivery.WarehouseId = request.WarehouseId;
            delivery.Consignee = request.Consignee;
            delivery.Operator = request.Operator;
            delivery.CustomerName = request.CustomerName;
            delivery.Phone = request.Phone;
            delivery.DocumentStatus = request.DocumentStatus;
            delivery.Auditor = request.Auditor;
            delivery.AuditDate = request.AuditDate;
            delivery.DeliveryDate = request.DeliveryDate;
            delivery.BusinessStatusId = request.BusinessStatusId;
            delivery.FreightCost = request.FreightCost;
            delivery.HandlingFee = request.HandlingFee;
            delivery.Address = request.Address;
            delivery.Remark = request.Remark;
            delivery.IsDeleted = false;

            var add1= deliverRep.Add(delivery).Result;
            if (add1 > 0)
            {
                var currentDateTimeStr = DateTime.Now.ToString("yyyyMMddHHmmss");
                long currentDateTimeLong = long.Parse(currentDateTimeStr);
                request.OrderMaterials.ForEach(c =>
                {
                    c.SaleCode = delivery.NoticeId;
                    c.MaterialsCode = currentDateTimeLong;
                });
                int add2 = materialBiz.AddRanges(request.OrderMaterials).Result;
                if (add2 > 0)
                {
                    result.Code = APIEnum.添加成功;
                    result.Msg = APIEnum.添加成功.ToString();
                }
                else
                {
                    result.Code = APIEnum.添加失败;
                    result.Msg = APIEnum.添加失败.ToString();
                }
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
            }

            return Task.FromResult(result);
        }
    }
}
